//
//  ChatListViewController.m
//  ChatForHot
//
//  Created by FLY on 2018/10/9.
//  Copyright © 2018年 FLY. All rights reserved.
//

#import "ChatListViewController.h"
#import "VisitorListViewController.h"
#import "ChatViewController.h"
#import "ChatHeaderView.h"
#import "GuestModel.h"

@interface ChatListViewController ()

@property (nonatomic, strong) ChatHeaderView *HOTCHATheaderView;

@end

@implementation ChatListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = NSLocalizedString(@"StrN046", nil);
    
}
- (void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:animated];
    
    [self HOTCHATInsertSubViews];
    
    [self refreshAndSortView];
    
    [self tableViewDidTriggerHeaderRefresh];
}
- (void)HOTCHATInsertSubViews{
    
    
    self.HOTCHATheaderView.frame = CGRectMake(0, 0, CGRectGetWidth(self.view.frame), 44);
    self.tableView.tableHeaderView = self.HOTCHATheaderView;

    self.tableView.backgroundColor = HOTCHATrgb(247, 247, 247);
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.tableView mas_updateConstraints:^(MASConstraintMaker *make) {
        
        make.edges.equalTo(self.view);
        make.bottom.equalTo(self.view).offset(-HOTCHATTabMenuHeight);
    }];

}
//rewirite delegate
#pragma mark - tableview

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 65;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if (self.delegate && [self.delegate respondsToSelector:@selector(conversationListViewController:didSelectConversationModel:)]) {
        EaseConversationModel *model = [self.dataArray objectAtIndex:indexPath.row];
        [self.delegate conversationListViewController:self didSelectConversationModel:model];
    } else {
        EaseConversationModel *model = [self.dataArray objectAtIndex:indexPath.row];
        ChatViewController *viewController = [[ChatViewController alloc] initWithConversationChatter:model.conversation.conversationId conversationType:model.conversation.type];
        viewController.title = model.userInfoModel.nickName.length > 0 ? model.userInfoModel.nickName : model.conversation.conversationId;
        [self.navigationController pushViewController:viewController animated:YES];
    }
}
- (void)HOTCHATsearchUserInfo:(UserInfoModel *)userInfoModel imUser:(NSString *)imUser{
    if ([userInfoModel HOTCHATgetIMImfoFromDBByImUser:imUser]) {
        
        return;
    }
    NSMutableDictionary *para = [[NSMutableDictionary alloc] init];
    [para setValue:imUser forKey:@"accountNumber"];
    
    [NetWorkTool HOTCHATRequestWithStringURL:ACCOUNTNUMBERGETACCOUNTINFO_URL progress:^(NSProgress * _Nonnull progress) {
        
    } RequestWay:@"GET" Parma:para SuccessBlock:^(BOOL isSucess, NSDictionary * _Nonnull resultDictionary) {
        if ([resultDictionary[@"code"] integerValue] == 0) {
            if (userInfoModel) {
                [userInfoModel setValuesForKeysWithDictionary:resultDictionary[@"data"]];
                [userInfoModel HOTCHATsaveIMUserInfo];
            }

            
        }
    } FailureBlock:^(NSError * _Nonnull error) {
        
    }];
    
    
    
}
-(void)HOTCHATrefreshGuestListData{
    
    self.HOTCHATheaderView.HOTCHATguestArr = [GuestModel HOTCHATgetTopThree];
}
//lazzy
- (ChatHeaderView *)HOTCHATheaderView{
    
    if (_HOTCHATheaderView == nil) {
        
        _HOTCHATheaderView = [[ChatHeaderView alloc]init];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(HOTCHATheaderViewTap)];
        [_HOTCHATheaderView addGestureRecognizer:tap];
    }
    return _HOTCHATheaderView;
}
- (void)HOTCHATheaderViewTap{
    
    VisitorListViewController *visitorVC = [[VisitorListViewController alloc]init];
    [self.navigationController pushViewController:visitorVC animated:YES];
}
@end
